package com.zzyl.nursing.service.impl;

import java.time.format.DateTimeFormatter;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.domain.entity.SysRole;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import com.zzyl.nursing.vo.AlertDataVo;
import com.zzyl.nursing.vo.AlertPageData;
import com.zzyl.system.mapper.SysRoleMapper;
import com.zzyl.system.mapper.SysUserMapper;
import io.jsonwebtoken.lang.Strings;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.xml.stream.events.StartDocument;
import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Override
    public void handleAlertData(LoginUser loginUser, HandleAlertDataDto dto) {

        SysUser user = loginUser.getUser();

        AlertData alertById = this.getById(dto.getId());

        LambdaUpdateWrapper<AlertData> wrapper = Wrappers.<AlertData>lambdaUpdate()
                .eq(AlertData::getIotId, alertById.getIotId())
                .eq(AlertData::getFunctionId, alertById.getFunctionId())
                .set(AlertData::getProcessorId, user.getDeptId())
                .set(AlertData::getProcessorName, user.getUserName())
                .set(AlertData::getStatus, 1);
        this.update(wrapper);
    }

    @Override
    public AlertPageData selectAlertData(AlertDataVo vo) {

        SysUser user = SecurityUtils.getLoginUser().getUser();

        List<SysRole> roles = user.getRoles();
        boolean flag = true;

        for (int i = 0; i < roles.size(); i++) {
            String roleKey = roles.get(i).getRoleKey();
            if (StringUtils.equals(Constants.SUPER_ADMIN, roleKey)) {
                flag = false;
            }
        }
        LambdaQueryWrapper<AlertData> wrp = Wrappers.<AlertData>lambdaQuery()
                .eq(vo.getStatus() != null, AlertData::getStatus, vo.getStatus())
                .eq(vo.getDeviceName() != null, AlertData::getDeviceName, vo.getDeviceName())
                .eq(flag, AlertData::getUserId, user.getUserId())
                .between(vo.getStartTime() != null && vo.getEndTime() != null, AlertData::getCreateTime, vo.getStartTime(), vo.getEndTime());

        IPage<AlertData> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        IPage<AlertData> pageResult = this.page(page, wrp);
        AlertPageData alertPageData = new AlertPageData();
        alertPageData.setPageSize(pageResult.getSize());
        alertPageData.setTotal(Long.toString(pageResult.getTotal()));
        alertPageData.setPages(Long.toString(pageResult.getPages()));
        alertPageData.setPage(pageResult.getPages());
        alertPageData.setRecords(pageResult.getRecords());

        return alertPageData;
    }

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {

        SysUser user = SecurityUtils.getLoginUser().getUser();

        List<SysRole> roles = user.getRoles();

        roles.forEach(r -> {
            if (!StringUtils.equals(Constants.SUPER_ADMIN, r.getRoleKey())) {
                alertData.setUserId(user.getUserId());
            }
        });

        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }


}
